package com.bckj.fastboot.paid.biz.interfaces.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.bckj.fastboot.core.sql.SqlQueryBuilder;
import com.bckj.fastboot.core.sql.StatementBuilder;
import com.bckj.fastboot.core.sql.meta.Column;
import com.bckj.fastboot.core.sql.meta.Metadata;
import com.bckj.fastboot.flow.process.BpmnProcessExecutor;
import com.bckj.fastboot.paid.api.entity.PayConfig;
import com.bckj.fastboot.paid.api.entity.PayOrder;
import com.bckj.fastboot.paid.api.model.PayConfigQuery;
import com.bckj.fastboot.paid.api.model.TestTxAsyncModel;
import com.bckj.fastboot.paid.biz.application.service.TestTxAsyncService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class TestController {

    private final BpmnProcessExecutor bpmnProcessExecutor;
    private final TestTxAsyncService testTxAsyncService;

    @SaIgnore
    @GetMapping("execute")
    public String execute(String processId) {
        bpmnProcessExecutor.execute(processId, null);
        return "execute";
    }

    @SaIgnore
    @GetMapping("test")
    public String test(String processId) {
        bpmnProcessExecutor.test(processId);
        return "test";
    }

    @SaIgnore
    @GetMapping("test2")
    public String test2(String taskId) {
        TestTxAsyncModel model = new TestTxAsyncModel();
        model.setTaskId(taskId);
        testTxAsyncService.test(model);
        return "test2";
    }

    @SaIgnore
    @GetMapping("test3")
    public String test3(String taskId) {
        TestTxAsyncModel model = new TestTxAsyncModel();
        model.setTaskId(taskId);
        testTxAsyncService.test3(model);
        return "test33";
    }

    @SaIgnore
    @GetMapping("test4")
    public String test4(PayConfigQuery query) {
        Metadata metadata = new Metadata();
        var payConfigT = metadata.addTable(PayConfig.class, "pc");
        var payOrderT = metadata.addTable(PayOrder.class, "po");
        Column poId = payOrderT.column("id");
        Column poName = payOrderT.column("name", "poName");
        Column poAge = payOrderT.column("age");
        Column pcId = payConfigT.column("id", "pcId");
        Column pcSex = payConfigT.column("sex");
        var sql = new StatementBuilder(metadata)
                .select(payConfigT)
                .select(payOrderT)
                .from(payOrderT)
                .innerJoin(payConfigT, poId, pcId)
                .leftJoin(payConfigT, poAge, pcSex)
                .groupBy(poName, pcSex)
                .orderBy(true, poAge, pcSex)
                .eq(PayConfig::getPayMerchantConfig, "a")
                .query(query);
        return sql.toString();
    }

    @SaIgnore
    @GetMapping("test5")
    public String test5(PayConfigQuery query) {
        var payConfigT = Metadata.table(PayConfig.class, "pc");
        var payOrderT = Metadata.table(PayOrder.class, "po");
        Column poId = payOrderT.column("id");
        Column poName = payOrderT.column("name", "poName");
        Column poAge = payOrderT.column("age");
        Column pcId = payConfigT.column("id", "pcId");
        Column pcSex = payConfigT.column("sex");
        var sql = new SqlQueryBuilder()
                .select(payConfigT)
                .select(payOrderT)
                .from(payOrderT)
                .innerJoin(payConfigT, poId, pcId)
                .leftJoin(payConfigT, poAge, pcSex)
                .groupBy(poName, pcSex)
                .orderBy(true, poAge, pcSex)
                .query(query);
        return sql.toString();
    }
}
